import * as Storage from '@/common/storage.js'

export const getNowTime = () => {
  return Math.ceil(Date.now() / 1000)
}

export const formatPrice = number => {
  number = parseFloat(number)
  return '￥' + number.toFixed(2)
}

export const formatNumber = number => {
  number = number.toString()
  return number[1] ? number : '0' + number
}

export const formatDuration = number => {
  const result = '00:00'
  if (number > 0) {
    const hours = Math.floor(number / 3600)
    const minutes = Math.floor((number - hours * 3600) / 60)
    const seconds = number % 60
    const format = []
    if (hours > 0) {
      format.push(formatNumber(hours))
    }
    if (minutes > 0) {
      format.push(formatNumber(minutes))
    }
    if (seconds >= 0) {
      format.push(formatNumber(seconds))
    }
    return format.join(':')
  }
  return result
}

export const thumbAvatar = url => {
  return `${url}!avatar_160`
}

export const thumbCover = url => {
  return `${url}!cover_270`
}

export const thumbSlide = url => {
  return `${url}!slide_1100`
}

export const orderStatus = status => {
  const mapping = {
    '1': '待支付',
    '2': '发货中',
    '3': '已完成',
    '4': '已关闭',
    '5': '已退款'
  }
  return mapping[status] ? mapping[status] : '未知'
}

export const refundStatus = status => {
  const mapping = {
    '1': '待处理',
    '2': '已取消',
    '3': '退款中',
    '4': '已拒绝',
    '5': '已完成',
    '6': '已失败'
  }
  return mapping[status] ? mapping[status] : '未知'
}

export const courseLevel = level => {
  const mapping = {
    '1': '入门',
    '2': '初级',
    '3': '中级',
    '4': '高级'
  }
  return mapping[level] ? mapping[level] : '未知'
}

export const redirect = (url, params = {}) => {
  const tabUrls = [
    '/pages/index/index',
    '/pages/course/category',
    '/pages/discovery/index',
    '/pages/me/index',
    '/pages/im/index'
  ]
  if (Object.keys(params).length > 0) {
    url += '?' + httpBuildQuery(params)
  }
  if (tabUrls.indexOf(url) !== -1) {
    uni.switchTab({
      url: url
    })
  } else if (url.indexOf('://') !== -1) {
    plus.runtime.openURL(url)
  } else if (url.indexOf('/pages') === 0) {
    uni.navigateTo({
      url: url
    })
  } else {
    uni.showToast({
      title: '不支持的协议类型'
    })
  }
}

export const getPlatform = () => {
  let platform = 'OTHER'
  // #ifdef H5
  platform = 'H5'
  // #endif
  // #ifdef MP-WEIXIN
  platform = 'MP_WEIXIN'
  // #endif
  // #ifdef MP-ALIPAY
  platform = 'MP_ALIPAY'
  // #endif
  // #ifdef MP-TOUTIAO
  platform = 'MP_TOUTIAO'
  // #endif
  // #ifdef APP-PLUS
  platform = uni.getSystemInfoSync().platform
  // #endif
  return platform.toLowerCase()
}

const httpBuildQuery = params => {
  let result = ''
  if (Object.keys(params).length > 0) {
    const arr = []
    for (const key in params) {
      arr.push(`${key}=${params[key]}`)
    }
    result = arr.sort().join('&')
  }
  return result
}

export const getApiBaseUrl = () => {
  return process.env.VUE_APP_BASE_API
}

export const getToken = () => {
  const key = Storage.cacheKey.token
  return Storage.get(key)
}

export const setToken = value => {
  const key = Storage.cacheKey.token
  return Storage.set(key, value)
}

export const clearToken = value => {
  const key = Storage.cacheKey.token
  return Storage.set(key, '')
}

export const isLogin = () => {
  if (getToken() === '') {
    return false
  }
  return true
}

export const checkLogin = (opt = {}) => {
  let redirect = '/pages/index/index'
  if (opt.redirect && opt.redirect !== '') {
    redirect = opt.redirect
  }
  if (getToken() === '') {
    uni.navigateTo({
      url: '/pages/auth/login?redirect=' + encodeURIComponent(redirect)
    })
    return false
  }
  if (opt.success && typeof opt.success === 'function') {
    opt.success()
  }
}
